package api

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/app/model/commonmodel/userEnum"
	"admin100/app/service"
	"admin100/app/service/centerservice"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var UserMgr = userMgrApi{}

type userMgrApi struct{}

// @summary 获取用户信息
// @tags    用户管理
// @produce json
// @router  /user/info [GET]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *userMgrApi) Info(r *ghttp.Request) {
	u := service.Context.GetUser(r.Context())
	platformList := service.Platform.GetPlatformListByUserId(u)
	platFormIdList := make([]string, 0, len(platformList))
	for _, v := range platformList {
		platFormIdList = append(platFormIdList, v.Id)
	}

	gameServerList := centerservice.CGameServer.GetServerList(platFormIdList)
	channelList, err := dao.Channel.Where(
		dao.Channel.Columns.PlatformId, platFormIdList).FindAll()
	defErr.IfPanic(err, define.DataOptFail)

	response.JsonSuc(r, struct {
		Name           string      `json:"name"`
		MenuTree       interface{} `json:"menuTree"`
		PlatformList   []*model.Platform
		ChannelList    []*model.Channel
		GameServerList []interface{}
		IsSuper        int
	}{
		Name:           u.Name,
		MenuTree:       service.Menu.TranMenuList2MenuTree(service.Menu.GetMenuListByUserId(u)),
		PlatformList:   platformList,
		GameServerList: gameServerList,
		ChannelList:    channelList,
		IsSuper:        u.IsSuper,
	})
}

// @summary 获取用户列表
// @tags 用户管理
// @produce json
// @param   page query int true "当前页数"
// @param   nums query int true "每页数据数量"
// @router  /user/list [GET]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *userMgrApi) List(r *ghttp.Request) {
	var req *model.MyadminUserListReq
	if err := r.Parse(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	pageInfo, data, err := service.User.List(req.Page, req.Nums)
	if err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	var res []*model.MyadminUserListResponseData
	for _, user := range data {
		var resData = model.MyadminUserListResponseData{}
		if err := gconv.Struct(user, &resData); err != nil {
			defErr.Panic(define.ReqParamsIllegal)
		}

		// 添加role info
		resData.RoleId = service.User.FetchRoleByUid(resData.Id)
		res = append(res, &resData)
	}

	response.JsonSuc(r, map[string]interface{}{
		"total":   pageInfo.Total,
		"pageNum": pageInfo.PNum,
		"info":    res,
	})
}

// @summary 增加用户
// @tags    用户管理
// @produce json
// @param   name formData  string true "用户名称"
// @param   account formData  string true "帐户名"
// @param   mobile formData  string false "手机号"
// @param   mailStr formData  string false "邮箱"
// @param   isSuper formData  int false "是否是超级管理员"
// @param   status formData  string true "状态"
// @param   roleId formData  string true "角色ID, 格式 3,55,44"
// @param   password formData  string true "密码"
// @router  /user/add [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *userMgrApi) Add(r *ghttp.Request) {
	//todo 判断是否有权限添加用户
	var (
		req         model.MyadminUserApiAddReq
		serviceData model.MyadminUserServiceAddData
	)
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	// 添加用户
	_ = service.User.Add(&serviceData)

	// 默认认定角色都存在,且传过来有值
	// 添加角色关联
	response.JsonSuc(r, service.User.AddRoleRel(serviceData.Id, req.RoleId))
}

// @summary 删除用户
// @tags 用户管理
// @produce json
// @param   uid query int64 true "用户ID"
// @router  /user/del [GET]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *userMgrApi) Del(r *ghttp.Request) {
	//todo 判断是否有权限添加用户

	// 这里就不判断角色不存在了
	var req = model.MyadminUserApiDelReq{}
	if err := r.ParseQuery(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	response.JsonSuc(r, service.User.Del(req.Uid))
}

// @summary 编辑用户
// @tags    用户管理
// @produce json
// @param   uid formData  string false "用户ID,不需要上传"
// @param   name formData  string true "用户名称"
// @param   account formData  string true "帐户名"
// @param   mobile formData  string false "手机号"
// @param   mailStr formData  string false "邮箱"
// @param   isSuper formData  int false "是否是超级管理员"
// @param   status formData  string true "状态"
// @param   roleId formData  string true "角色ID, 格式 3,55,44"
// @param   password formData  string true "密码"
// @router  /user/edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *userMgrApi) Edit(r *ghttp.Request) {
	//todo 判断是否有权限添加用户
	var (
		req  = model.MyadminUserApiEditReq{}
		data = model.MyadminUserServiceAddData{}
	)

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}
	if err := gconv.Struct(req, &data); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	_ = service.User.Edit(&data)
	//写入session
	if req.Status != userEnum.Enabled {
		sessUser := service.User.FetchUserByUid(req.Id)
		service.Session.SetUser(r.Context(), sessUser)
	}

	response.JsonSuc(r, service.User.AddRoleRel(req.Id, req.RoleId))
}

// @summary 用户列表
// @tags    用户管理
// @produce json
// @router  /user/simple-list [POST]
// @success 200 {object} response.JsonResponse "用户列表"
func (a *userMgrApi) SimpleList(r *ghttp.Request) {
	res := service.User.GetSimpleList()
	response.JsonSuc(r, res)
}
